package com.gvitz.transaction.activities;



import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.gvitz.transaction.R;
import com.gvitz.transaction.database.webserviceCards;
import com.gvitz.transaction.utils.CreditCardValidation;
import com.gvitz.transaction.utils.crouton.Crouton;
import com.gvitz.transaction.utils.crouton.Style;
import com.gvitz.transaction.utils.validations.Field;
import com.gvitz.transaction.utils.validations.Form;
import com.gvitz.transaction.utils.validations.IsJustL;
import com.gvitz.transaction.utils.validations.NotEmpty;


public class RegisterCardActivity extends GVActivity  {
 
  
	    private EditText mName;
	    private EditText mNumber;
	    private TextView mSeparator;
	    private Spinner mYear;
	    private Spinner mMes;
	    private Spinner mType;
	    TextView  fecha;
	    Activity act = this;
	    private Button mSubmit;

	    // Form used for validation
	    private Form mForm;

	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_register_card);
	        initFields();
	        initValidationForm();
	        initCallbacks();

	    }


	    private void initFields() {
	       
	    	mName = (EditText) findViewById(R.id.name);	
		    mNumber = (EditText) findViewById(R.id.number);
		    mMes = (Spinner) findViewById(R.id.mes_spinner);
		    mYear = (Spinner) findViewById(R.id.year_spinner);
		    mType = (Spinner) findViewById(R.id.type_spinner);
	        mSubmit = (Button) findViewById(R.id.submit_card);	
	        mSeparator = (TextView)findViewById(R.id.separator);
	        
	       Typeface font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome-webfont.ttf" );
	        
	       TextView t1 = (TextView) findViewById(R.id.icon_card_numero);
	       TextView t2 = (TextView) findViewById(R.id.icon_card_nombre);
	       TextView t3 = (TextView) findViewById(R.id.icon_card_spinner);
	       fecha = (TextView) findViewById(R.id.icon_card_fecha);

	       t1.setTypeface(font);
	       t2.setTypeface(font);
	       t3.setTypeface(font);
	       fecha.setTypeface(font);


	        
	    	List<String> list3 = new ArrayList<String>();
	    	list3.add("Tarjeta Credito");
	    	list3.add("Tarjeta Debito");
	    	
	    	List<String> list1 = new ArrayList<String>();
	    	list1.add("01");
	    	list1.add("02");
	    	list1.add("03");
	    	list1.add("04");
	    	list1.add("05");
	    	list1.add("06");
	    	list1.add("07");
	    	list1.add("08");
	    	list1.add("09");
	    	list1.add("10");
	    	list1.add("11");
	    	list1.add("12");	    	
	    	
	    	List<String> list2 = new ArrayList<String>();
	    	list2.add("2015");
	    	list2.add("2016");
	    	list2.add("2017");
	    	list2.add("2018");
	    	list2.add("2019");
	    	list2.add("2020");
	    	list2.add("2021");	
	    	list2.add("2022");	
	    	list2.add("2023");	
	    	list2.add("2024");	
	    	list2.add("2025");	
	    	list2.add("2026");	
	    	list2.add("2027");	
	    	list2.add("2028");	
	    	list2.add("2029");
	    	

	    	
	    	ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list1);
	    	dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    	mMes.setAdapter(dataAdapter1);
	    	
	    	ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list2);
	    	dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    	mYear.setAdapter(dataAdapter2);
	    	
	    	ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list3);
	    	dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    	mType.setAdapter(dataAdapter3);
	    	
	    	mType.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					if(mType.getSelectedItem().toString().equals("Tarjeta Debito")){
						
						fecha.setVisibility(View.GONE);
						mYear.setVisibility(View.GONE);
						mMes.setVisibility(View.GONE);
						mSeparator.setVisibility(View.GONE);
						mYear.setSelection(10);
						mMes.setSelection(0);
						
						
					}
					else{
						fecha.setVisibility(View.VISIBLE);
						mYear.setVisibility(View.VISIBLE);
						mMes.setVisibility(View.VISIBLE);	
						mSeparator.setVisibility(View.VISIBLE);
					}
					
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}


	    	});
	    	

	        
	    }

	    private void initValidationForm() {
	        mForm = new Form(this);
	        mForm.addField(Field.using(mName).validate(NotEmpty.build(this)).validate(IsJustL.build(this)));
	        mForm.addField(Field.using(mNumber).validate(NotEmpty.build(this)));


	    }

	    private void initCallbacks() {

	        mSubmit.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View view) {
	                submit();
	            }
	        });
	    }
	    
	    private void submit() {
	        if (mForm.isValid()) {
	        	if(mNumber.length() > 13){
			        		
	        			try {
			        			
			        			SharedPreferences prefs=  getSharedPreferences("preferencias", Context.MODE_PRIVATE);    	        		
		    	        		//enviar la informacion al servidor
		    	        		webserviceCards w = new webserviceCards(getApplicationContext(), this);
		    	        			        			
			        			if(mType.getSelectedItem().toString().equals("Tarjeta Credito")){
			        				
			        				long input = Long.parseLong(mNumber.getText().toString());
				                   
			        				if (CreditCardValidation.isValid(input) == true) {	    	        		
				    	        		w.insertCardC(prefs.getString("id_usuario",""), 
				    	        						mNumber.getText().toString(),
				    	        						mMes.getSelectedItem().toString(),
				    	        						mYear.getSelectedItem().toString(),
				    	        						CreditCardValidation.getType(input), 
				    	        						mName.getText().toString());     
					                } else {
					                	Crouton.makeText(this, "Tarjeta Invalida", Style.ALERT).show();
					                }
			        			}
			        			else{
			    	        		
			        				w.insertCardD(prefs.getString("id_usuario",""), 
			        						mNumber.getText().toString(),
			        						mName.getText().toString());  	        				
			        			}
			        			
		
								
							} catch (Exception e) {
								Crouton.makeText(this, "Tarjeta Invalida", Style.ALERT).show();
							}
	        		
	        	}else{
	        			Crouton.makeText(this, getString(R.string.zvalidations_not_number), Style.ALERT).show();	        			
	        	}
	        }
	    }
	    

	    
	    
		@Override
		public void onBackPressed() {
			Intent i = new Intent(this, MainActivity.class);
			overridePendingTransition (R.anim.open_next, R.anim.close_main);
			startActivity(i);
			finish();
			return;
		}
	    

	}
         
   	